package com.tianji.learning.listener;

import com.tianji.api.dto.trade.OrderBasicDTO;
import com.tianji.common.constants.MqConstants;
import com.tianji.common.utils.BeanUtils;
import com.tianji.common.utils.CollUtils;
import com.tianji.learning.domain.LearningLesson;
import com.tianji.learning.service.LearningLessonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description LessonChangeListener
 * @Author WJH
 * @Date 2024-12-19
 */

@Component
@Slf4j
public class LessonChangeListener {
    @Autowired
    private LearningLessonService learningLessonService;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "learning.lesson.pay.queue",durable = "true"),
    exchange = @Exchange(name = MqConstants.Exchange.ORDER_EXCHANGE,type = ExchangeTypes.TOPIC),
    key = MqConstants.Key.ORDER_PAY_KEY))
    public void addLesson(OrderBasicDTO order){
        if(order.getOrderId()==null || order==null || CollUtils.isEmpty(order.getCourseIds())){
            log.error("接收到MQ消息有误，订单数据为空");
            return;
        }
        LearningLesson learningLesson = BeanUtils.copyBean(order, LearningLesson.class);
        learningLessonService.save(learningLesson);
    }
}
